package cfig.ota

import cfig.helper.ZipHelper.Companion.getEntryOffset
import org.apache.commons.compress.archivers.zip.ZipFile
import org.apache.commons.compress.archivers.zip.ZipMethod
import org.junit.Test

import org.junit.Assert.*

class BrilloPropStringTest {
    @Test
    fun testMetaItem() {
        val m1 = BrilloProp("filename", 200L, 512L)
        assertEquals(m1.toString(), "filename:200:512")

        val m2 = BrilloProp("filename", 0, 0)
        assertEquals(m2.toString(), "filename               ")
    }

    @Test
    fun computeEntryOffset() {
        val dpi140 = this.javaClass.classLoader.getResource("140dpi.zip").file
        ZipFile(dpi140).use { zf ->
            //211  Defl:X      181  14% 05-23-2019 19:04 af40929c  holo/holo_wallpaper_edittext.png
            val entry = zf.getEntry("holo/holo_wallpaper_edittext.png")
            assertEquals(ZipMethod.DEFLATED.ordinal, entry.method)
            assertEquals(181, entry.compressedSize)
            assertEquals(211, entry.size)
            assertEquals(84482, entry.getEntryOffset())
        }

        ZipFile(dpi140).use { zf ->
            //1928  Stored     1928   0% 05-23-2019 19:04 a6e74244  holo/holo_wallpaper_progressbar_horizontal_100.png
            val entry = zf.getEntry("holo/holo_wallpaper_progressbar_horizontal_100.png")
            assertEquals(ZipMethod.STORED.ordinal, entry.method)
            assertEquals(1928, entry.compressedSize)
            assertEquals(1928, entry.size)
            assertEquals(113296, entry.getEntryOffset())
        }
    }

    @Test
    fun computeEntryOffsetSize() {
        val dpi140 = this.javaClass.classLoader.getResource("140dpi.zip").file
        ZipFile(dpi140).use { zf ->
            //211  Defl:X      181  14% 05-23-2019 19:04 af40929c  holo/holo_wallpaper_edittext.png
            assertEquals(BrilloProp(
                    "holo_wallpaper_edittext.png", 84482, 211),
                    BrilloProp(zf, "holo/holo_wallpaper_edittext.png"))
        }
    }

    @Test
    fun fromZipFile() {
        val dpi140 = this.javaClass.classLoader.getResource("140dpi.zip").file
        println("[" + BrilloPropString().preCompute(dpi140) + "]")
    }
}
